package sfmc.biz.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;

import sfmc.biz.service.SfmcCustStatusService;
import sfmc.biz.vo.Sfmc_TB_CUSTSTATUS;
import sfmc.biz.vo.Sfmc_TB_EMPLOYEE;
import sfmc.common.util.DateUtil;

@Controller
public class SfmcCustStatusController {
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	@Autowired 
	private SfmcCustStatusService sfmcCustStatusService; 
	
	@RequestMapping(value="/custStatus_list.do")
	public ModelAndView custStatusList(ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_CUSTSTATUS sfmc_TB_CUSTSTATUS){
		
		if("".equals(sfmc_TB_CUSTSTATUS.getSearchFromDate())){
			sfmc_TB_CUSTSTATUS.setSearchFromDate(DateUtil.getCurrentTime("yyyyMMdd"));
			sfmc_TB_CUSTSTATUS.setSearchToDate(DateUtil.getCurrentTime("yyyyMMdd"));
		}else{
			sfmc_TB_CUSTSTATUS.setSearchFromDate(sfmc_TB_CUSTSTATUS.getSearchFromDate().replaceAll("-", ""));
			sfmc_TB_CUSTSTATUS.setSearchToDate(sfmc_TB_CUSTSTATUS.getSearchToDate().replaceAll("-", ""));
		}
		
		List<Sfmc_TB_CUSTSTATUS> custStatusList = sfmcCustStatusService.getCustStatusList(sfmc_TB_CUSTSTATUS);
		
		Gson gson = new Gson();
		mav.addObject("custStatusList", gson.toJson(custStatusList));
		mav.addObject("custstatusdata", sfmc_TB_CUSTSTATUS);
		mav.setViewName("/custstatus/custstatus_list");
		System.out.println("CTL custStatusList : "+custStatusList);
		return mav;
	}
	
	
	@RequestMapping(value="/custstatus_excel.do")
	public void listsToExcel(HttpServletResponse resp, OutputStream ost,ModelAndView mav, HttpSession session, HttpServletRequest request, Sfmc_TB_CUSTSTATUS sfmc_TB_CUSTSTATUS) throws Exception{
		
		FileOutputStream fos = null;
		
		List<Sfmc_TB_CUSTSTATUS> excelList = sfmcCustStatusService.getCustStatusList(sfmc_TB_CUSTSTATUS);
		resp.setHeader("Content-Disposition", "attachment; filename=OrderListByCustName.xls");
		
		HSSFWorkbook workbook = new HSSFWorkbook();
		HSSFSheet firstSheet  = workbook.createSheet("sheet1");
		
		for(int i=0; i<excelList.size(); i++){
			
			HSSFRow rowHeader = firstSheet.createRow(0);
			HSSFCell headerA = rowHeader.createCell(0);
			HSSFCell headerB = rowHeader.createCell(1);
			HSSFCell headerC = rowHeader.createCell(2);
			HSSFCell headerD = rowHeader.createCell(3);
			headerA.setCellValue(new HSSFRichTextString("날짜"));
			headerB.setCellValue(new HSSFRichTextString("업체명"));
			headerC.setCellValue(new HSSFRichTextString("전화번호"));
			headerD.setCellValue(new HSSFRichTextString("고객코드"));
			
			sfmc_TB_CUSTSTATUS = excelList.get(i);
			HSSFRow rowA = firstSheet.createRow(i);
			HSSFCell cells1 = rowA.createCell(0); 
			HSSFCell cells2 = rowA.createCell(1);
			HSSFCell cells3 = rowA.createCell(2);
			HSSFCell cells4 = rowA.createCell(3);
			cells1.setCellValue(sfmc_TB_CUSTSTATUS.getOrderDate());
			cells2.setCellValue(sfmc_TB_CUSTSTATUS.getCustName());
			cells3.setCellValue(sfmc_TB_CUSTSTATUS.getCustPhone());
			cells4.setCellValue(sfmc_TB_CUSTSTATUS.getCustCode());
		}
		try{
        	workbook.write(ost);
        	ost.flush();
        	fos = new FileOutputStream(new File("C:/OrderListByCustName.xls"));
        	workbook.write(fos);
        }
        catch(Exception e){
        	log.fatal(e.getMessage()+"\n");
        	log.fatal(e.getStackTrace());
        }
        finally{
        	if(fos != null){
        		try{
	        		fos.flush();
	                fos.close();
        		}
        		catch(IOException e){
        			log.fatal(e.getMessage());
        		}
        	}
        }
	}
	
	

}
